import json
import os


class Test():

    def read_log(self, file_path):
        with open(file_path, 'r', encoding="utf-8") as f:
            for line in f.read().splitlines():
                store_id = OrderNo = posNo = payType = chnlType = tranType = RespCode = tranDate = ""
                if "callMisPosPaymentApi model:{" in line:
                    _l = line.split("callMisPosPaymentApi model:")[1]
                    _json = json.loads(_l)
                    store_id = _json["posNo"][:4]
                    OrderNo = _json["merTradeNo"]
                    posNo = _json["posNo"]
                    payType = _json["payType"]
                    chnlType = "SCO"
                    tranType = _json["tranType"]
                    print(f"{store_id}, {OrderNo}, {posNo}, {payType}, {chnlType}, {tranType},")
                    # with open("./sco.txt", 'a', encoding="utf-8") as f1:
                    #     f1.write(f"{store_id}, {OrderNo}, {posNo}, {payType}, {chnlType}, {tranType},")
                    #     f1.close()
                if "调用支付" in line:
                    _l = line.split("- 调用支付")[1]
                    _l = _l[4:]
                    _json = json.loads(_l)
                    respCode = _json["respCode"]
                    try:
                        rcDetail = _json["rcDetail"]
                    except:
                        rcDetail = ""

                    try:
                        tranDate = _json["tranDate"]
                    except:
                        tranDate = str(line[:4]) + str(line[5:7]) + str(line[8:10]) + str(line[11:13]) + str(line[14:16]) +str(line[17:19])
                    print(f"{respCode}, {tranDate}, {rcDetail} \n")
                    # with open("./sco.txt", 'a', encoding="utf-8") as f2:
                    #     f2.write(f"{respCode}, {tranDate}, {rcDetail} \n")
                    #     f2.close()
                # print(store_id, OrderNo, posNo, payType, chnlType, tranType, RespCode, tranDate)

    def run(self):
        base_path = r"D:\Disktop\新建文件夹 (5)\1027"
        for root, dir, fils in os.walk(base_path):
            for fil in fils:
                abs_path = os.path.join(root, fil)
                self.read_log(abs_path)


if __name__ == '__main__':
    # file = r'D:\Disktop\新文件 2.txt'
    # file = r'D:\Disktop\新建文件夹\1009-SCO\SHGS9SC8androidAco.2024-01-25.0.log'
    t = Test()
    # t.read_log(file)
    t.run()